let app = getApp();
const extConfig =  app.siteInfo;
const isCompanyCloudTemplate = Number(extConfig.isCloudTemplate) === 1 ? true : false;

Component({
  properties: {
    parent: {
      type: Object,
      value: {},
    },
  },
  methods: {
    navigateToModulePage(pagePath, params) {
      if (isCompanyCloudTemplate) {
        app.util.request({
          url: 'entry/wxapp/api',
          data: { type: 16 },
          module: 'ws_micro_company',
          success: (res) => {
            if (res.data && res.data.data) {
              const templateOrder = Number(res.data.data.template_order);
              pagePath = pagePath.replace(/ws_micro_company/i, `ws_micro_company_${templateOrder === 0 ? 'visual' : templateOrder}`);
              app.util.navigateTo(pagePath, params || '', false);
            }
          },
          fail: (res) => {
            console.log(`get template order fail`, res);
          },
        });
      } else {
        app.util.navigateTo(pagePath, params || '', false);
      }
    },
    _onNavigator(e) {
      const module = app.siteInfo.module;
      let pagePath = e.currentTarget.dataset && e.currentTarget.dataset.pagepath;
      const pageType = e.currentTarget.dataset && e.currentTarget.dataset.pagetype;
      if (pagePath && (pageType === '1' || pageType === '4')) {
        if (module === 'ws_micro_shop') {
          // 兼容微商城后台配置的轮播、导航页面路径
          if (/^\/pages/.test(pagePath)) {
            if (pagePath.indexOf('/book/') > 0) {
              pagePath = `ws_micro_shop_book${pagePath}`;
            } else if (pagePath.indexOf('/pt/') > 0) {
              pagePath = `ws_micro_shop_pt${pagePath}`;
            } else if (pagePath.indexOf('/miaosha/') > 0) {
              pagePath = `ws_micro_shop_ms${pagePath}`;
            } else {
              pagePath = `ws_micro_shop${pagePath}`;
            }
          }
          if (/^\/pages\/list\/list/.test(pagePath)) {
            pagePath = `${pagePath}&integral=0`;
          }
        }
        this.navigateToModulePage(pagePath);
      } else if (pagePath && pageType === '3') {
        this.navigateToModulePage(`/${module}/pages/web/web`, { url: pagePath, webview_url: pagePath });
      }
    },
    _makePhoneCall(e) {
      let { phonenumber } = e.currentTarget.dataset;
      let { mobile, landline } = app.util.phoneExp;
      if (mobile.test(String(phonenumber)) || landline.test(String(phonenumber))) {
        wx.makePhoneCall({
          phoneNumber: String(phonenumber),
          success: res => {
            console.log('wx.makePhoneCall success:', res);
          },
          fail: res => {
            console.log('wx.makePhoneCall fail:', res);
          },
        });
      } else {
        console.log('电话号码错误 ======= ', phonenumber);
      }
    },
  },
  ready() {
    // 添加links数据长度使其长度与images数组长度相同
    let magicData = this.data.parent;
    console.log(magicData);
    let imagesLinks = [];
    if (magicData && magicData.images) {
      let images = magicData.images;
      let links = magicData.links || [];
      for (let i in images) {
        if (Object.prototype.hasOwnProperty.call(images, i)) {
          let obj = { image: images[i], link: links[i] || null };
          imagesLinks.push(obj);
        }
      }
    }
    this.setData({
      imagesLinks,
    });
    console.log('imagesLinks', imagesLinks);
  },
});
